package com.nhimeye.admin.ui;

import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;

import com.nhimeye.admin.app.AbstractEntityView;
import com.nhimeye.admin.app.EntityEditor;
import com.nhimeye.admin.app.EntityProviderUtil;
import com.nhimeye.admin.app.H1;
import com.nhimeye.domain.ContentVersion;
import com.nhimeye.domain.ItemType;
import com.nhimeye.service.ItemTypeService;
import com.vaadin.addon.jpacontainer.EntityItem;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.spring.roo.addon.annotations.RooVaadinEntityView;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;


public class ItemTypeView extends AbstractEntityView<ItemType> {

	@Autowired
	ItemTypeService itemTypeService;
	
	private JPAContainer<ItemType> tableContainer;

    @Override
    protected void configureTable(Table table) {
        table.setContainerDataSource(getTableContainer());
        table.setVisibleColumns(getTableColumns());

        
    }

	@Override
	protected HashSet<String> getIgnoreTableColumns() {
		return new HashSet<String>();
	}

	@Override
	protected Class<? extends ItemType> getEntityClass() {
		 return ItemType.class;
	}

	@Override
	protected String getEntityName() {
		return "Item Type";
	}

	@Override
	public boolean isCreateAllowed() {
		return true;
	}

	@Override
	protected boolean isUpdateAllowed() {
		return true;
	}

	@Override
	protected boolean isDeleteAllowed() {
		return true;
	}

	@Override
	protected boolean isNewEntity(ItemType entity) {
		return (entity != null && getIdForEntity(entity) == null);
	}

	@Override
	protected void deleteEntity(ItemType entity) {
		if (entity != null) {
			itemTypeService.deleteItemType (entity);
	        }
		
	}

	@Override
	protected ItemType saveEntity(ItemType entity) {
		if (entity == null) {
            return null;
        }
		itemTypeService.saveItemType(entity);
         return entity;
	}

	@Override
	public Object getIdProperty() {
		 return "id";
	}

	@Override
	public Object getVersionProperty() {
		 return "version";
	}

	@Override
	protected Container getTableContainer() {
		if (tableContainer == null) {
            JPAContainer<ItemType> container = new JPAContainer<ItemType>(ItemType.class);
            container.setEntityProvider(EntityProviderUtil.get().getEntityProvider(ItemType.class));
            tableContainer = container;
        }
        return tableContainer;	}

	@Override
	protected Item getItemForEntity(ItemType entity) {
		 Item item = getTable().getItem(entity.getId());
	        if (item == null) {
	            item = tableContainer.createEntityItem(entity);
	        }
	        return item;
	}

	@Override
	protected ItemType getEntityForItem(Item item) {
		if (item instanceof EntityItem && ((EntityItem) item).getEntity() instanceof ItemType) {
            return (ItemType) ((EntityItem) item).getEntity();
        } else {
            return null;
        }
	}

	@Override
	public Object getIdForEntity(ItemType entity) {
		return entity != null ? entity.getId() : null;
	}

	@Override
	protected ItemType createEntityInstance() {
		return new ItemType();	}

	@Override
	protected void configureButton(Button b) {
		b.setCaption("+ Add Type");
	}

	@Override
	protected void configureCaption(H1 title) {
		title.setCaption("ItemTypes");
	}

	@Override
	protected EntityEditor createForm() {
		return new ItemTypeForm();
	}

}
